package com.thinkcar.core.exception.extension;

import com.thinkcar.core.exception.api.BaseErrorCode;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;

/**
 * @author: jiangyong
 * @date: 2025/10/18
 * @description: BaseException
 */
public abstract class BaseException extends RuntimeException {

    private static final long serialVersionUID = -4165273660283203899L;
    /**
     * 映射错位码
     */
    private String code;
    /**
     * 映射错误信息
     */
    private String msg;

    protected BaseException() {
    }

    /**
     * 异常类构造方法，通过错误码映射错误信息
     */
    public BaseException(BaseErrorCode baseErrorCode) {
        super(handleMessage(baseErrorCode.getCode(), baseErrorCode.getMsg()));
        this.code = baseErrorCode.getCode();
        this.msg = baseErrorCode.getMsg();
    }

    /**
     * 异常类构造方法，通过错误码映射错误信息
     */
    public BaseException(BaseErrorCode baseErrorCode, Object... parameters) {
        super(handleMessage(baseErrorCode.getCode(), MessageFormatter.arrayFormat(baseErrorCode.getMsg(), parameters).getMessage()));
        this.code = baseErrorCode.getCode();
        this.msg = MessageFormatter.arrayFormat(baseErrorCode.getMsg(), parameters).getMessage();
    }

    /**
     * 异常类构造方法，通过错误码映射错误信息
     */
    public BaseException(BaseErrorCode baseErrorCode, Boolean skip, Object... parameters) {
        super(handleMessage(baseErrorCode.getCode(), handleErrorMessage(baseErrorCode, skip, parameters)));
        this.code = baseErrorCode.getCode();
        this.msg = handleErrorMessage(baseErrorCode, skip, parameters);
    }

    /**
     * 异常类构造方法，直接传入异常信息，异常吗和响应信息
     */
    public BaseException(String errorCode, String errorMessage) {
        super(handleMessage(errorCode, errorMessage));
        this.code = errorCode;
        this.msg = errorMessage;
    }

    /**
     * 异常类构造方法，直接传入异常信息，异常吗和响应信息
     */
    public BaseException(String errorCode, String errorMessage, Object... parameters) {
        super(handleMessage(errorCode, MessageFormatter.arrayFormat(errorMessage, parameters).getMessage()));
        this.code = errorCode;
        this.msg = MessageFormatter.arrayFormat(errorMessage, parameters).getMessage();
    }

    /**
     * 异常类构造方法，直接传入异常信息，异常吗和响应信息
     */
    public BaseException(String errorCode, String errorMessage, Boolean skip, Object... parameters) {
        super(handleMessage(errorCode, handleMessage(skip, errorMessage, parameters)));
        this.code = errorCode;
        this.msg = handleMessage(skip, errorMessage, parameters);
    }

    /**
     * 构造函数
     */
    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    /**
     * 构造函数
     */
    public BaseException(Throwable cause) {
        super(cause);
    }

    public String getErrorCode() {
        return code;
    }

    public void setErrorCode(String code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return msg;
    }

    public void setErrorMessage(String msg) {
        this.msg = msg;
    }

    private static String handleMessage(String errorCode, String errorMessage) {
        return (ObjectUtils.isEmpty(errorCode)) ? errorMessage : errorCode + ":" + errorMessage;
    }

    /**
     * @author admin
     * 处理响应信息
     */

    private static String handleMessage(Boolean skip, String errorMessage, Object... parameters) {
        String erroMsg = errorMessage;

        if (skip && null != parameters && parameters.length != 0) {
            erroMsg = Arrays.toString(parameters);
        } else if (!skip) {
            erroMsg = MessageFormatter.arrayFormat(errorMessage, parameters).getMessage();
        }

        return erroMsg;

    }

    /**
     * @author admin
     * 处理异常信息
     */
    private static String handleErrorMessage(BaseErrorCode baseErrorCode, Boolean skip, Object... parameters) {

        if (parameters == null) {
            return baseErrorCode.getMsg();
        } else if (null != skip && skip) {
            return Arrays.toString(parameters);
        }

        return MessageFormatter.arrayFormat(baseErrorCode.getMsg(), parameters).getMessage();

    }

}
